/* ********************************************************************************************
*  Alvin Baldemeca
*  Tetris Project
*  Course: TCSS 305 , Alan Fowler
*  Term: Spring 2012
*  UNIVERISTY OF WASHINGTON TACOMA
*
*
**********************************************************************************************/

package gui.actions;



import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;


import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Timer;

import view.TetrisBoardPanel;





/**
 *This action tells a Board to pause the game.
 *@author Alvin Baldemeca
 *@version 5/25/2012
 */
@SuppressWarnings("serial")
public class PauseAction extends AbstractAction
{
  
  /**
   * The panel to perform the action to.
   */
  private final Timer my_timer;
  
  /**
   * The tetris board panel to set focus on and off, to perform the required pause.
   */
  private final TetrisBoardPanel my_panel;
  
  /**
   * Creates a PauseAction.
   * @param the_timer the tetris timer to pause
   * @param the_panel The tetris board panel to set focus on and off,
   *  to perform the required pause
   */
  public PauseAction(final Timer the_timer, final TetrisBoardPanel the_panel) 
  {
    super("Pause");
    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
    putValue(Action.SELECTED_KEY, true);
    my_timer = the_timer;
    my_panel = the_panel;
  }

  @Override
  public void actionPerformed(final ActionEvent the_event) 
  {
    if (!my_panel.isStateInitial())
    {
      enablePause();
    }
  }
  
  /**
   * Enables the pause action if the game has started otherwise the button is not enable.
   */
  private void enablePause()
  {
    if (my_timer.isRunning())
    {
      my_timer.stop();
      my_panel.setPause(true);
      my_panel.setFocusable(false);
      my_panel.repaint();
    }
    else
    {
      my_timer.start();
      my_panel.setPause(false);
      my_panel.setFocusable(true);
      my_panel.requestFocus();
    }
  }
  
  






}
